package com.glwan.fixedassetapp.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.Map;

public class MapAdapter extends ArrayAdapter<Map.Entry<String, String>> {

    public MapAdapter(Context context, Map<String, String> map) {
        super(context, 0, map.entrySet().toArray(new Map.Entry[0]));
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_spinner_item, parent, false);
        }

        Map.Entry<String, String> entry = getItem(position);

        TextView textView = convertView.findViewById(android.R.id.text1);
        textView.setText(entry.getValue()); // 显示 Map 的键

        return convertView;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);
        }

        Map.Entry<String, String> entry = getItem(position);

        TextView textView = convertView.findViewById(android.R.id.text1);
        textView.setText("名称："+entry.getValue() + " | 编码：" + entry.getKey()); // 显示 Map 的键

        return convertView;
    }
}
